package com.joekai.base_library.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.joekai.base_library.http.HttpRetrofitRequest;

import java.lang.reflect.Type;
import java.util.List;

/**
 * author:  JoeKai
 * date: 2022/7/1 02:05
 * content：
 */
public class GsonUtil {
    private volatile static GsonUtil INSTANCES;

    public static GsonUtil getInstances() {
        if (INSTANCES == null) {
            synchronized (HttpRetrofitRequest.class) {
                if (INSTANCES == null) {
                    INSTANCES = new GsonUtil();
                }
            }
        }
        return INSTANCES;
    }

    public <T> T partObj(String s, Type rawType) {
        T target = build().fromJson(s, rawType);
        return target;
    }

    public <T> T partList(String s, Type... typeArguments) {
        Type type = TypeToken.getParameterized(List.class, typeArguments).getType();

        T target = build().fromJson(s, type);
        return target;
    }

    private Gson build() {
        Gson gson = new Gson();
        return gson;
    }

}
